package com.wu.dms.controller;

import cn.hutool.core.date.DateUtil;
import com.wu.common.convertion.result.Result;
import com.wu.common.convertion.result.Results;
import com.wu.constant.StatisConstant;
import com.wu.dms.constant.CountryEnum;
import com.wu.dao.mapper.*;
import com.wu.dms.dao.mapper.*;
import com.wu.dms.dto.req.PlayStatisticDataReq;
import com.wu.dms.dto.resp.PageResp;
import com.wu.dms.service.SongRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cousin
 * @date 2024/12/26
 * @Description
 */
@RestController()
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class StatisticController {
    private final SongMapper songMapper;
    private final SingerMapper singerMapper;
    private final SongListMapper songListMapper;
    private final UserMapper userMapper;
    private final DmsSongRecordMapper songRecordMapper;
    private final SongRecordService songRecordService;
    @GetMapping("/song")
    public Result<Long> songStatistics(){
        return Results.success(songMapper.selectCount());
    }
    @GetMapping("/singer")
    public Result<Map<String,Long>> singerStatistics(){
        Map<String,Long> resp =new HashMap<>();
        long femalesCount = 0;
        long malesCount = 0;
        long bandCount = 0;
        long groupStarCount = 0;
        long count = 0L;
        List<Map<String, Object>> maps = singerMapper.selectAllStatistics();
        for (Map<String, Object> map : maps) {
            long females = Long.parseLong(map.get("females").toString());
            long males = Long.parseLong(map.get("males").toString());
            long band = Long.parseLong(map.get("band").toString());
            long groupStar = Long.parseLong(map.get("groupStar").toString());
            long locationCount = Long.parseLong(map.get("locationCount").toString());
            String location = map.get("location").toString();
            String country = CountryEnum.getCountryByLocation(location);
            bandCount += band;
            groupStarCount += groupStar;
            femalesCount += females;
            malesCount += males;
            count += locationCount;
            resp.put(country, resp.getOrDefault(country, 0L) + locationCount);
        }
        resp.put("count",count);
        resp.put("femalesCount", femalesCount);
        resp.put("malesCount", malesCount);
        resp.put("bandCount", bandCount);
        resp.put("groupStarCount", groupStarCount);
        return Results.success(resp);
    }
    @GetMapping("/songList")
    public Result<Map<String,Long>> songListStatistics(){
        List<Map<String, Object>> maps = songListMapper.selectAllStatistics();
        long sum = 0;
        Map<String,Long> resp = new HashMap<>();
        for (Map<String, Object> map : maps) {
            long styleCount =Long.parseLong(map.get("styleCount").toString());
            sum += styleCount;
            String style = map.get("style").toString();
            resp.put(style, resp.getOrDefault(style, 0L) + styleCount);
        }
        resp.put("count",sum);
        return Results.success(resp);
    }
    @GetMapping("user")
    public Result<Map<String,Long>> getUserStatistics(){
       Map<String,Object> maps = userMapper.selectAllStatistics();
        long males = Long.parseLong(maps.get("males").toString());
        long females = Long.parseLong(maps.get("females").toString());
        long count = males  + females;
        Map<String,Long> resp = new HashMap<>();
        resp.put("count", count);
        resp.put("malesCount", males);
        resp.put("femalesCount", females);
        return Results.success(resp);
    }
    @PostMapping("/uploadSongPlayData")
    public Result<Void> getSongPlayData(@RequestBody PlayStatisticDataReq dataReq){
        Date now =new Date();
        String nowFormat = DateUtil.format(now, StatisConstant.TIME_FORMAT);
        Long id = dataReq.getSongId();
        int duration = dataReq.getDuration() / 1000;
        int valid = 0;
        if (duration >= StatisConstant.PLAY_VALID_TIME){
            valid =1;
        }
        songRecordMapper.updateCount(id,valid,nowFormat);
        return Results.success();
    }
    @GetMapping("/getSongPlayData")
    public Result<PageResp> getSongPlayData(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,
                                            @RequestParam String searchWord, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime){
        searchWord = "%"+searchWord+"%";
        List<Map<String, Object>> maps = songRecordMapper.selectPlayStatistic(pageNum, pageSize, searchWord, beginTime, endTime);
        PageResp result = new PageResp();
        result.setTotal(songRecordMapper.countPlayStatistic(searchWord, beginTime, endTime));
        result.setData(maps);
        return Results.success(result);
    }
}
